package com.pxmeta.app.controller.community;

import cn.dev33.satoken.annotation.SaCheckDisable;
import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Maps;
import com.pxmeta.bot.service.IBotService;
import com.pxmeta.comment.entity.Comment;
import com.pxmeta.comment.entity.dto.CommentDTO;
import com.pxmeta.comment.entity.dto.CommentLikeDTO;
import com.pxmeta.comment.entity.vo.CommentVO;
import com.pxmeta.comment.service.ICommentService;
import com.pxmeta.comment.service.ICommentUserLikeService;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.utils.SecurityUtils;
import com.pxmeta.common.utils.ThreadPoolExecutorUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;


/**
 * 参考 Soul 设计，只有两级评论，通过评论时间降序。
 */
@RestController
@RequestMapping("/community/comment")
@Api(tags = "评论", value = "评论")
@RequiredArgsConstructor
public class CommentController {

    private final ICommentService commentService;
    private final ICommentUserLikeService userLikeService;
    private final IBotService botService;

    @ApiOperation("根据动态id获取评论列表")
    @GetMapping("/getComments")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "dynamicId", value = "动态id"),
                    @ApiImplicitParam(name = "page", value = "页码"),
                    @ApiImplicitParam(name = "size", value = "每页记录数"),
                    @ApiImplicitParam(name = "type", value = "动态类型：1最新，2热门"),
                    @ApiImplicitParam(name = "parentId", value = "父评论id")
            }
    )
    public AjaxResult<List<CommentVO>> getComment(@RequestParam Long dynamicId,
                                                  @RequestParam Integer page,
                                                  @RequestParam Integer size,
                                                  @RequestParam(defaultValue = "1", required = false) Integer type,
                                                  @RequestParam(required = false, defaultValue = "0") Long parentId) {
        Long userId = 0L;
        try {
            userId = SecurityUtils.getUserId();
        } catch (Exception ignored) {
        }

        List<CommentVO> list = commentService.getByDynamicId(userId, dynamicId, page, size, type, parentId);
        return AjaxResult.success(list);
    }


    @SaCheckLogin
    @ApiOperation("评论")
    @PostMapping("/comment")
    @SaCheckDisable
    public AjaxResult comment(@RequestBody CommentDTO dto) {
        Long userId = SecurityUtils.getUserId();
        commentService.comment(dto, userId);

        return AjaxResult.success();
    }

    @SaCheckLogin
    @ApiOperation("删除评论")
    @DeleteMapping("/removeComment")
    public AjaxResult removeComment(@RequestParam Long commentId) {
        commentService.removeComment(commentId);
        return AjaxResult.success();
    }

    @SaCheckLogin
    @ApiOperation("点赞/取消点赞")
    @PostMapping("/like")
    @SaCheckDisable
    public AjaxResult like(@RequestBody @Valid CommentLikeDTO dto) {
        Long userId = SecurityUtils.getUserId();
        userLikeService.like(dto, userId);
        return AjaxResult.success();
    }

    @SaCheckLogin
    @ApiOperation("查询是否喜欢")
    @GetMapping("/isLike")
    public AjaxResult isLike(@RequestParam Long commentId) {
        Long userId = SecurityUtils.getUserId();
        Map<String, Object> map = Maps.newHashMap();
        map.put("like", userLikeService.isLike(userId, commentId));
        return AjaxResult.success(map);
    }

}
